# -*- coding: utf-8 -*-
from pymongo import MongoClient


class MyMongoClient:
    def __init__(self, host, port, server_port=None, ip=None, *args,
                 **kwargs):
        #  标志 D J A 区分
        self.client = MongoClient(host=host, port=int(port))

        self.id_pools = self.client.guidang4.id_pools
        self.capture = self.client.guidang4.capture
        self.card = self.client.guidang4.card
        self.cardacvs = self.client.guidang4.cardacvs
        self.cardface = self.client.guidang4.cardface
        self.record = self.client.guidang4.record
        self.insert_fail = self.client.guidang4.insert_fail
        self.config = self.client.guidang4config.config
        self.logcount_ = self.client.guidang4.logcount
        self.acvs = self.client.guidang4.acvs

        if ip is not None:
            self.ip = ip
            self.server_port = server_port

        # 贺昌浩
        self.run_mongo_time = self.client.guidang4.runmongo_time

    def insert_fail_img(self, data_dic):
        self.insert_fail.insert_one(data_dic)

    def drop_all(self):
        self.client.drop_database(self.client.guidang4)

    def del_card_mongo(self, card_id, acvs_list):
        self.capture.delete_one({"_id": card_id})
        self.card.delete_one({"_id": card_id})
        cardacvs_res = self.cardacvs.delete_many({"card_id": card_id})
        print("cardacvs_count:", cardacvs_res.deleted_count)
        cardface_res = self.cardface.delete_many({"card_id": card_id})
        print("cardface_count:", cardface_res.deleted_count)
        for acvs_id in acvs_list:
            self.acvs.update_one({"_id": acvs_id}, {"$inc": {"lencard": -1}})

    def init_mongo(self):

        if self.logcount_.find_one({'_id': 'logCount'}) is None:
            self.logcount_.insert_one(
                {
                    "_id": "logCount",
                    "count": 1
                }
            )

        if 'id_pools' not in self.client.guidang4.list_collection_names():
            self.id_pools.insert_one({
                '_id': 'idpools',
                'face_id': 1
            })

    def get_hezi_face_id(self):
        return self.id_pools.find_one_and_update({'_id': 'idpools'}, {'$inc': {'face_id': 1}}, upsert=True)['face_id']

    def ins_capture(self, data):
        self.capture.insert_one(data)

    def updata_capture(self, id, feature, name, person_id, uri):
        self.capture.update_one({'_id': id}, {
            '$set': {'ovn_f': feature, 'name': name, 'person_id': person_id, 'face_image_uri': uri}}, upsert=True)

    def get_record(self):
        return list(self.record.find())

    # 贺昌浩
    def insert_cap(self, dic):
        id = dic.pop('_id')
        return self.capture.update_one({'_id': id}, {'$set': dic}, upsert=True)

    def update_card_info(self, card_id, face_image_uri, name, person_id):
        self.card.update_one({'_id': card_id},
                             {'$set': {'face_image_uri': face_image_uri, 'name': name, 'person_id': person_id}})

        self.cardacvs.update_many({'face_image_id': card_id},
                                  {'$set': {'name': name, 'person_id': person_id, 'face_image_uri':
                                      face_image_uri}})

        self.cardface.update_many({'card_id': card_id},
                                  {'$set': {"card_person_id": person_id, "card_face_image_uri": face_image_uri,
                                            "card_picture_uri": face_image_uri, "card_name": name}})
